package cool.goodbai.service.impl;

import cool.goodbai.entity.User;
import cool.goodbai.example.UserExample;
import cool.goodbai.handler.DataProxy;
import cool.goodbai.mapper.UserMapper;
import cool.goodbai.service.UserService;
import cool.goodbai.util.StringUtil;
import cool.goodbai.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DataProxy dataProxy;

    @Override
    public User login(String username, String password) {
        UserExample example = new UserExample();
        example.createCriteria().andUsernameEqualTo(username).andPasswordEqualTo(password);
        List<User> user = userMapper.selectByExample(example);
        if (user.size() == 1) {
            return user.get(0);
        } else {
            throw new RuntimeException();
        }
    }

    @Override
    public User insert(User user) {
        UserExample example = new UserExample();
        example.createCriteria().andUsernameEqualTo(user.getUsername());
        List<User> users = userMapper.selectByExample(example);
        if (users.size() == 0) {
            dataProxy.preInsert(user);
            userMapper.insertSelective(user);
            return userMapper.selectByPrimaryKey(user.getId());
        } else {
            throw new RuntimeException();
        }
    }

    @Override
    public User register(User user) {
        UserExample example = new UserExample();
        // 创建查询条件
        example.createCriteria().andUsernameEqualTo(user.getUsername());
        List<User> users = userMapper.selectByExample(example);
        if (users.size() == 0) {
            user.setUtcCreate(new Date());
            user.setUtcModify(user.getUtcCreate());
            userMapper.insertSelective(user);
            return userMapper.selectByPrimaryKey(user.getId());
        } else {
            throw new RuntimeException();
        }
    }

    @Override
    public User delete(Long id) {
        // 通过主键查询id
        User user = userMapper.selectByPrimaryKey(id);
        if (user != null) {
            userMapper.deleteByPrimaryKey(id);
            return user;
        } else {
            throw new RuntimeException();
        }
    }

    @Override
    public User update(User user) {
        User old = userMapper.selectByPrimaryKey(user.getId());
        if (old != null) {
            dataProxy.preUpdate(user);
            userMapper.updateByPrimaryKeySelective(user);
            return user;
        } else {
            throw new RuntimeException();
        }
    }

    @Override
    public PageResult<List<User>> select(User user, Integer current, Integer pageSize) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        //用户名
        if (!Objects.isNull(user.getId())) {
            criteria.andIdEqualTo(user.getId());
        }
        //用户名
        if (StringUtil.isNotEmpty(user.getUsername())) {
            criteria.andUsernameLike("%" + user.getUsername() + "%");
        }
        //姓名
        if (StringUtil.isNotEmpty(user.getName())) {
            criteria.andNameLike("%" + user.getName() + "%");
        }
        //电话
        if (StringUtil.isNotEmpty(user.getPhone())) {
            criteria.andPhoneLike("%" + user.getPhone() + "%");
        }
        //性别
        if (StringUtil.isNotEmpty(user.getSex())) {
            criteria.andSexEqualTo(user.getSex());
        }
        //身份证号码
        if (StringUtil.isNotEmpty(user.getIdentityNum())) {
            criteria.andIdentityNumLike("%" + user.getIdentityNum() + "%");
        }
        //邮箱
        if (StringUtil.isNotEmpty(user.getEmail())) {
            criteria.andEmailLike("%" + user.getEmail() + "%");
        }
        //名族
        if (StringUtil.isNotEmpty(user.getNation())) {
            criteria.andNationLike("%" + user.getNation() + "%");
        }
        //政治面貌
        if (StringUtil.isNotEmpty(user.getPoliticsStatus())) {
            criteria.andPoliticsStatusEqualTo(user.getPoliticsStatus());
        }
        //婚姻情况
        if (StringUtil.isNotEmpty(user.getMaritalStatus())) {
            criteria.andMaritalStatusEqualTo(user.getMaritalStatus());
        }
        //毕业院校
        if (StringUtil.isNotEmpty(user.getSchool())) {
            criteria.andSchoolLike("%" + user.getSchool() + "%");
        }
        //所学专业
        if (StringUtil.isNotEmpty(user.getMajor())) {
            criteria.andMajorLike("%" + user.getMajor() + "%");
        }
        //最高学历
        if (StringUtil.isNotEmpty(user.getEducation())) {
            criteria.andEducationEqualTo(user.getEducation());
        }
        //角色
        if (StringUtil.isNotEmpty(user.getAccess())) {
            criteria.andAccessEqualTo(user.getAccess());
        }

        // 分页
        long total = userMapper.countByExample(example);
        if (current != null && pageSize != null) {
            example.setLimit(pageSize);
            // 设置偏移量
            example.setOffset(((current.longValue() - 1) * pageSize));
        }
        List<User> users = userMapper.selectByExample(example);
        return new PageResult<>(users, current, pageSize, true, total, 0);
    }
}
